#include "Tile.h"
#include "GameObject.h"
#include "Game.h"

#include <algorithm>
#include <typeinfo>
#include <vector>


Tile::Tile(int x, int y) :
	x(x),
	y(y)
{
}


Tile::~Tile()
{
	for (GameObjects::iterator it = gameObjects.begin(); it != gameObjects.end(); ++it)
	{
		delete *it;
	}
}


void Tile::add(GameObject* gameObject)
{
	gameObject->setLocation(x, y);

	// Notify collisions.
	for (GameObjects::iterator it = gameObjects.begin(); it != gameObjects.end(); ++it)
	{
		(*it)->handleCollision(gameObject);
		gameObject->handleCollision(*it);
	}

	gameObjects.push_back(gameObject);
}


void Tile::remove(const GameObject* gameObject)
{
	// Find the object and remove it.
	gameObjects.erase(std::find(gameObjects.begin(), gameObjects.end(), gameObject));
}


bool Tile::isPassable(const GameObject* gameObject) const
{
	for (GameObjects::const_iterator it = gameObjects.begin(); it != gameObjects.end(); ++it)
	{
		if (!(*it)->canBePassed(gameObject))
		{
			return false;
		}
	}
	return true;
}


void Tile::getUnPassableObjects(GameObjects& unPassableGameObjects, const GameObject* gameObject) const
{
	for (GameObjects::const_iterator it = gameObjects.begin(); it != gameObjects.end(); ++it)
	{
		if (!(*it)->canBePassed(gameObject))
		{
			unPassableGameObjects.push_back(*it);
		}
	}
}


void Tile::draw() const
{
	for (GameObjects::const_iterator it = gameObjects.begin(); it != gameObjects.end(); ++it)
	{
		(*it)->draw();
	}
}